package com.huluobo.high3videoproject.mvp.fullscreen.view;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

import android.content.res.Resources;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.SlidingDrawer;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.huluobo.base.view.BaseActivity;
import com.huluobo.high3videoproject.R;
import com.huluobo.high3videoproject.adapter.CommentAdapter;
import com.huluobo.high3videoproject.adapter.VideoFullscreenAdapter;
import com.huluobo.high3videoproject.bean.CommentBeanResult;
import com.huluobo.high3videoproject.bean.FollowBean;
import com.huluobo.high3videoproject.bean.LikeBean;
import com.huluobo.high3videoproject.bean.SendCommentBeanResult;
import com.huluobo.high3videoproject.bean.VideoBeanResult;
import com.huluobo.high3videoproject.mvp.fullscreen.presenter.VideoFullscreenContract;
import com.huluobo.high3videoproject.mvp.fullscreen.presenter.VideoFullscreenPresenter;
import com.huluobo.high3videoproject.mvp.video.presenter.VideoContract;
import com.huluobo.high3videoproject.mvp.video.presenter.VideoPresenter;
import com.huluobo.high3videoproject.net.Constant;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
import com.shuyu.gsyvideoplayer.video.base.GSYVideoView;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMVideo;
import com.umeng.socialize.media.UMWeb;

import java.util.ArrayList;
import java.util.List;

public class VideoActivity extends BaseActivity<VideoFullscreenContract.View, VideoFullscreenPresenter>
        implements VideoFullscreenContract.View {
    private VideoRecyclerView recyclerView;
    private VideoFullscreenAdapter adapter;

    private List<VideoBeanResult.VideoBean> videoBeans = new ArrayList<>();

    private PagerSnapHelper pagerSnapHelper;

    private LinearLayoutManager layoutManager;
    //当前播放的下标
    private int currentPosition = 0;

    //评论recyclerView
    private RecyclerView commentRecyclerView;

    //评论的adapter
    private CommentAdapter commentAdapter;

    //评论的数据源
    private List<CommentBeanResult.CommentBean> commentBeanList = new ArrayList<>();


    @Override
    public void onGetVideoListSuccess(VideoBeanResult videoBeanResult) {
        Log.i("TagA", "videoBeanResult:" + videoBeanResult.getData().size());
        if (videoBeanResult != null) {
            videoBeans.addAll(videoBeanResult.getData());
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onGetVideoListFailed(String error) {
        Log.i("TagA", "全屏activity中获取视频列表失败:" + error);
    }

    @Override
    public void onFollowSuccess(FollowBean followBean) {
        ToastUtils.showLong("感谢关注");
        //拿到当前的videoBean,可以进行修改其中的是否关注
        VideoBeanResult.VideoBean videoBean = adapter.getData().get(currentPosition);
        videoBean.setGuanzhu(1);
        //修改页面上的add按钮消失
        adapter.getViewByPosition(currentPosition, R.id.item_video_iv_add).setVisibility(View.GONE);
    }

    @Override
    public void onFollowFailed(String error) {
        Log.i("TagA", "关注失败:" + error);
    }

    @Override
    public void onLikeVideoSuccess(LikeBean likeBean) {
        ToastUtils.showLong("点赞成功");
        VideoBeanResult.VideoBean videoBean = adapter.getData().get(currentPosition);
        videoBean.setDianzan(1);
        ImageView imageView = (ImageView) adapter.getViewByPosition(currentPosition, R.id.item_video_iv_like_btn);
        imageView.setImageResource(R.drawable.ic_xihuanred);
    }

    @Override
    public void onLikeVideoFailed(String error) {
        Log.i("TagA", "点赞失败:" + error);
    }

    @Override
    public void onNoLikeVideoSuccess(LikeBean likeBean) {
        ToastUtils.showLong("取消点赞成功");
        VideoBeanResult.VideoBean videoBean = adapter.getData().get(currentPosition);
        videoBean.setDianzan(0);
        ImageView imageView = (ImageView) adapter.getViewByPosition(currentPosition, R.id.item_video_iv_like_btn);
        imageView.setImageResource(R.drawable.ic_xihuan);
    }

    @Override
    public void onNoLikeVideoFailed(String error) {
        Log.i("TagA", "取消点赞失败:" + error);
    }

    @Override
    public void onGetCommentListSuccess(CommentBeanResult commentBeanResult) {
        if (commentBeanResult != null && commentBeanResult.getData().size() != 0) {
            commentBeanList.addAll(commentBeanResult.getData());
            commentAdapter.notifyDataSetChanged();
        } else {
            ToastUtils.showLong("还没有人评论");
        }
    }

    @Override
    public void onGetCommentListFailed(String error) {
        Log.i("TagA", "error:" + error);
    }

    @Override
    public void onSendCommentSuccess(CommentBeanResult sendCommentBeanResult) {
        EditText editText = (EditText) adapter.getViewByPosition(currentPosition, R.id.item_video_edit);
        editText.setText("");//清空评论输入框
        if (sendCommentBeanResult != null) {
            commentBeanList.addAll(0, sendCommentBeanResult.getData());
            commentAdapter.notifyDataSetChanged();
            commentRecyclerView.scrollToPosition(0);
        }
    }

    @Override
    public void onSendCommentFailed(String error) {

    }

    @Override
    protected void initData() {
        presenter = new VideoFullscreenPresenter();
        presenter.getVideoList(2, 10);
    }

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.fullscreen_video_recycleView);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new VideoFullscreenAdapter(videoBeans);
        recyclerView.setAdapter(adapter);
        //设置仿抖音滑动效果
        pagerSnapHelper = new PagerSnapHelper();
        pagerSnapHelper.attachToRecyclerView(recyclerView);

        //滑动自动播放
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            //滑动状态改变监听器
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    //空闲状态,开始播放
                    View snapView = pagerSnapHelper.findSnapView(layoutManager);
                    //拿到展示item的position
                    currentPosition = layoutManager.getPosition(snapView);
                    //通过position拿到播放器
                    StandardGSYVideoPlayer standardGSYVideoPlayer =
                            (StandardGSYVideoPlayer) adapter.getViewByPosition(currentPosition,
                                    R.id.item_gsy_video_player);
                    //判断当前播放器没有在播放中
                    if (standardGSYVideoPlayer.getCurrentState() != GSYVideoView.CURRENT_STATE_PLAYING) {
                        standardGSYVideoPlayer.startPlayLogic();
                    }
                }
            }

        });

        //添加adapter中可点的id
        adapter.addChildClickViewIds(R.id.item_video_iv_add, R.id.item_video_iv_comment_btn,
                R.id.item_video_iv_like_btn, R.id.item_video_comment_close, R.id.item_video_iv_send,
                R.id.item_video_iv_share_btn);
        //添加adapter中item的view的点击事件
        adapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                VideoBeanResult.VideoBean videoBean =
                        (VideoBeanResult.VideoBean) adapter.getData().get(currentPosition);
                switch (view.getId()) {
                    case R.id.item_video_iv_add:
                        //有了position,还有数据源,就可以知道点击的是哪个video对象,然后从video对象中拿到需要关注的username
                        addFollow(videoBean);
                        break;
                    case R.id.item_video_iv_comment_btn:
                        showComment(videoBean);
                        break;

                    case R.id.item_video_iv_like_btn:
                        likeVideo(videoBean);
                        break;
                    case R.id.item_video_comment_close:
                        closeComment();
                        break;
                    case R.id.item_video_iv_send:
                        sendComment(videoBean);
                        break;

                    case R.id.item_video_iv_share_btn:
                        shareVideo(videoBean);
                        break;
                }
            }
        });
    }

    private void shareVideo(VideoBeanResult.VideoBean videoBean) {

        //设置分享缩略图
        UMImage umImage = new UMImage(this, videoBean.getVideomainimg());//图片地址
        umImage.compressStyle = UMImage.CompressStyle.SCALE;//压缩大小

        UMWeb web = new UMWeb(videoBean.getVideopath());
        web.setTitle(videoBean.getAuthname());//标题
        web.setThumb(umImage);//缩略图
        web.setDescription(videoBean.getCaption());//描述

        new ShareAction(this)
                .withMedia(web)
                .open();

    }

    //发送评论
    private void sendComment(VideoBeanResult.VideoBean videoBean) {
        EditText editText = (EditText) adapter.getViewByPosition(currentPosition, R.id.item_video_edit);
        String msg = editText.getText().toString().trim();
        if (TextUtils.isEmpty(msg)) {
            return;
        }
        presenter.sendComment(msg, videoBean.getId());
    }

    private void closeComment() {
        //放开事件拦截,让列表可以继续滑动
        recyclerView.setInterceptor(false);
        //隐藏评论区域
        LinearLayout layout = (LinearLayout) adapter.getViewByPosition(currentPosition, R.id.item_video_ll);
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_out);
        animation.setDuration(500);
        animation.setInterpolator(new AccelerateInterpolator());//加速度的插值器
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                layout.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        layout.startAnimation(animation);
    }

    //动画展示评论区域
    private void showComment(VideoBeanResult.VideoBean videoBean) {
        //可能需要拦截事件
        recyclerView.setInterceptor(true);
        //显示评论区域
        LinearLayout layout = (LinearLayout) adapter.getViewByPosition(currentPosition, R.id.item_video_ll);
        layout.setVisibility(View.VISIBLE);

        //初始化评论列表
        commentRecyclerView = (RecyclerView) adapter.getViewByPosition(currentPosition,
                R.id.item_video_comment_recyclerView);
        commentRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        commentAdapter = new CommentAdapter(R.layout.item_commet, commentBeanList);
        commentRecyclerView.setAdapter(commentAdapter);

        //执行位移动画
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_in);
        animation.setDuration(500);
        animation.setInterpolator(new AccelerateInterpolator());//加速度的插值器
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                //加载评论列表数据
                presenter.getCommentList(videoBean.getId());
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        layout.startAnimation(animation);
    }

    private void likeVideo(VideoBeanResult.VideoBean videoBean) {
        if (videoBean.getDianzan() == 0) {
            //等于0代表当前video没有点赞,所以需要调用点赞方法
            presenter.likeVideo(videoBean.getId());
        } else {
            presenter.noLikeVideo(videoBean.getId());
        }
    }

    private void addFollow(VideoBeanResult.VideoBean videoBean) {
        presenter.followUser(videoBean.getAuthname());
    }

    @Override
    protected int bindLayoutId() {
        return R.layout.activity_video;
    }

}